package com.manage.service.impl;



import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.manage.dao.IBaseDao;
import com.manage.po.Room;
import com.manage.service.IRoomService;
import com.manage.util.PagerModel;


@Service("RoomService")
@Transactional(propagation = Propagation.REQUIRED)
public class RoomService implements IRoomService{

	@Autowired
	private IBaseDao baseDao;

	@Override
	public Room getRoomById(String roomid) {
		// TODO Auto-generated method stub
		return baseDao.get(Room.class, roomid);
	}

	@Override
	public void merge(Room room) {
		// TODO Auto-generated method stub
		baseDao.merge(room);
	}

	@Override
	public PagerModel getRoomById(String roomid, int pageno, int pagesize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void save(Room room) {
		// TODO Auto-generated method stub
		baseDao.save(room);
	}

	@Override
	public void update(Room room) {
		// TODO Auto-generated method stub
		baseDao.update(room);
	}

	@Override
	public void delete(String roomid) {
		// TODO Auto-generated method stub
		Room room = getRoomById(roomid);
		baseDao.delete(room);
	}

	@Override
	public PagerModel search(String roomid, String roomname,String roomleader, int pageno,
			int pagesize) {
		// TODO Auto-generated method stub
		List<Criterion> cronlist = new ArrayList<Criterion>();
		if(roomid!=null && !roomid.equals(""))
			cronlist.add(Restrictions.like("roomid",roomid,MatchMode.ANYWHERE));
		if(roomname!=null && !roomname.equals(""))
			cronlist.add(Restrictions.like("roomname",roomname,MatchMode.ANYWHERE));
		if(roomleader!=null && !roomleader.equals(""))
			cronlist.add(Restrictions.like("roomleader",roomleader,MatchMode.ANYWHERE));
		return baseDao.findPageByCriterion(Room.class, pageno, pagesize, null, cronlist);
	}

	@Override
	public Room TestHql(String roomid) {
		// TODO Auto-generated method stub
		String hql="from Romm where roomid=?";//HQL语句中表名应该是ORM映射的类名,查询結果是实体类，而不是sql的结果集
		List<Room> list=baseDao.findByHql(hql, roomid);
		return list.get(0);
	}

	@Override
	public Room TestHql(String... param) {
		// TODO Auto-generated method stub
		String hql="from Room where roomid=?";
		List<Room> list=baseDao.findByHql(hql, param);
		return list.get(0);
	}

	@Override
	public void batchDelete(String[] ids) {
		// TODO Auto-generated method stub
		for(int i=0;i<ids.length;i++)
			delete(ids[i]);	
	}
		
	

}
